// pages/index/activity/activity.js
const app = getApp()
//获取 log 日志打印
const log = new app.appLogs()
//获取 http 网络请求
const http = new app.appHttp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //进行中
        haveinhand: [],
        //待开展
        carried: [],
        //已结束
        finished: [],
        imagewidth: 0, // 缩放后的宽
        imageheight: 0, // 缩放后的高
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            theme: app.globalData.theme
        })

        // this.getHaveinhand(1)
        this.getFinished(0)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    /**
     * 当前激活的标签改变时触发
     * @param event
     */
    onChange(event) {
        const than = this
        const num = event.detail.name
        // if (num == 0) {
        //     than.getHaveinhand(1)
        // } else if (num == 1) {
        //     than.getCarried(2)
        // } else if (num == 2) {
        //     than.getFinished(0)
        // }
        if (num == 0) {
            than.getFinished(0)
        } else if (num == 1) {
            than.getHaveinhand(1)
        } else if (num == 2) {
            than.getCarried(2)
        }
    },
    /**
     * 进行中详情
     */
    haveinhandonClick: function (event) {
        const id = event.currentTarget.dataset.id
        wx.navigateTo({
            url: "../activity/activityindex/activityindex?id="+id+"&type=0"
        })
    },
    /**
     * 待开展详情
     */
    carriedonClick: function (event) {
        const id = event.currentTarget.dataset.id
        wx.navigateTo({
            url: "../activity/activityindex/activityindex?id="+id+"&type=1"
        })
    },
    /**
     * 已结束详情
     */
    finishedonClick: function (event) {
        const id = event.currentTarget.dataset.id
        wx.navigateTo({
            url: "../activity/activityindex/activityindex?id="+id+"&type=2"
        })
    },
    /**
     * 请求进行中的活动
     * @param state
     */
    getHaveinhand: function (state) {
        const than = this
        http.get("activity/list", {
            area_code:app.globalData.areaCode,
            status: state,
            page_number: app.globalData.pageNumber,
            page_size: app.globalData.pageSize,
        }).then((res) => {
            if (res.data.code == 200) {
                than.setData({
                    haveinhand: res.data.data.list
                })
            }
        })
    },
    /**
     * 请求待开展的活动
     * @param state
     */
    getCarried: function (state) {
        const than = this
        http.get("activity/list", {
            area_code: app.globalData.areaCode,
            status: state,
            page_number: app.globalData.pageNumber,
            page_size: app.globalData.pageSize,
        }).then((res) => {
            if (res.data.code == 200) {
                than.setData({
                    carried: res.data.data.list
                })
            }
        })
    },
    /**
     * 请求已结束的活动
     * @param state
     */
    getFinished: function (state) {
        const than = this
        http.get("activity/list", {
            area_code: app.globalData.areaCode,
            status: state,
            page_number: app.globalData.pageNumber,
            page_size: app.globalData.pageSize,
        }).then((res) => {
            if (res.data.code == 200) {
                than.setData({
                    finished: res.data.data.list
                })
            }
        })
    },
    imageLoad: function (e) {
        var imageSize = this.imageUtil(e)
        this.setData({
            imagewidth: imageSize.imageWidth,
            imageheight: imageSize.imageHeight
        })
    },
    imageUtil: function (e) {
        var imageSize = {};
        var originalWidth = e.detail.width; // 图片原始宽
        var originalHeight = e.detail.height; // 图片原始高
        var originalScale = originalHeight/originalWidth; //图片高宽比
        //获取屏幕宽高
        wx.getSystemInfo({
            success: function (res) {
                var windowWidth = res.windowWidth;
                var windowHeight = res.windowHeight;
                var windowscale = windowHeight/windowWidth;//屏幕高宽比
                if (originalScale < windowscale) { // 图片高宽比小于屏幕高宽比
                    //图片缩放后的宽为屏幕宽
                    imageSize.imageWidth = windowWidth;
                    imageSize.imageHeight = (windowWidth * originalHeight) / originalWidth;
                } else { // 图片高宽比大于屏幕高宽比
                    //图片缩放后的高为屏幕高
                    imageSize.imageHeight = windowHeight;
                    imageSize.imageWidth = (windowHeight * originalWidth) / originalHeight;
                }
            }
        })
        return imageSize;
    }
})